/**
 * @Copyright：<a href="www.wisesoft.net.cn">四川川大智胜系统集成有限公司</a>
 * @Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
 * @CreateTime: 2022年08月04日  08:59
 */
package com.fxsen.commons.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;

/**
 *@Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
 *@CreateTime: 2022年08月04日  08:59
 */
public class BeanUtils {
    /**
     * map转为对象
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/4 8:59
 	 * @param: source
 	 * @param: target
 	 * @return: {@link T}
    **/
    public static <T> T mapToObj(Map<String, Object> source, Class<T> target) throws Exception {
        Field[] fields = target.getDeclaredFields();
        T o = target.newInstance();
        for(Field field:fields){
            Object val = source.get(field.getName());
            if((val=source.get(field.getName()))!=null){
                field.setAccessible(true);
                field.set(o,val);
            }
        }
        return o;
    }
    /**
     * 将values中的值设置到t对象中去,t必须是标准的javaBean
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:44
 	 * @param: t
 	 * @param: values
    **/
    public static <T> void invokeBeanProperty(T t, Map<String, Object> values) {
        try {
            if (values != null) {
                Iterator<String> it = values.keySet().iterator();
                Class<?> clazz = t.getClass();
                Method method = null;
                Field field = null;
                // 循环map,将map中的值设置到t对象中去
                while (it.hasNext()) {
                    String fieldName = it.next();
                    Object fieldValue = values.get(fieldName);
                    String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                    try {
                        field = clazz.getDeclaredField(fieldName);
                        // 对象没有此属性
                        if (field == null) {
                            continue;
                        }
                        // 实体没有此方法
                        method = clazz.getMethod(setMethodName, new Class[] { field.getType() });
                        if (method == null) {
                            continue;
                        }
                        // 执行操作,将值设置到t对象中去
                        method.invoke(t, new Object[] { fieldValue });
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static <T> void invokeBeanPropertyByString(T t, Map<String, String> values) {
        try {
            if (values != null) {
                Iterator<String> it = values.keySet().iterator();
                Class<?> clazz = t.getClass();
                Method method = null;
                Field field = null;
                // 循环map,将map中的值设置到t对象中去
                while (it.hasNext()) {
                    String fieldName = it.next();
                    String fieldValue = values.get(fieldName);
                    String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                    try {
                        field = clazz.getDeclaredField(fieldName);
                        // 对象没有此属性
                        if (field == null) {
                            continue;
                        }
                        // 实体没有此方法
                        method = clazz.getMethod(setMethodName, new Class[] { field.getType() });
                        if (method == null) {
                            continue;
                        }
                        // 执行操作,将值设置到t对象中去, 只处理类型为String.Integer.Date类型
                        if (field.getType().isAssignableFrom(Integer.class) || field.getType().isAssignableFrom(int.class)) {
                            method.invoke(t, new Object[] { Integer.parseInt(fieldValue) });
                        } else if (field.getType().isAssignableFrom(String.class)) {
                            method.invoke(t, new Object[] { fieldValue });
                        } else if (field.getType().isAssignableFrom(java.util.Date.class)) {
                            method.invoke(t, new Object[] { DateUtils.parseDate1(fieldValue) });
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
